<?php
namespace app\Backend\controller;;
use think\Db;
use clt\Leftnav;
use app\Backend\model\Pages;    //單頁
use think\Validate;
class Single extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    public function index()
    {
        return $this->fetch();
    }
    //=========================================== 單頁管理 ======================================================
    //單頁列表
    public function singleList(){

        $list = db('pages')->order('id desc')->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //删除單頁
    public function singleDel(){
        $id             = input('id');
        $map['id']      = $id;
        if (db('pages')->where($map)->delete()){
            return $result = ['code'=>1,'msg'=>'删除成功!','url'=>'/reload'];
        }else{
            return $result = ['code'=>0,'msg'=>'單頁删除失败!','url'=>''];
        }
    }

    //修改单页
    public function singleEdit(){
        if(request()->isPost()){
            $data   = input('post.');
            //验证标题是否唯一  156128
            $map['id']    = array('neq',input('post.id'));
            $where['id']  = array('eq',input('post.id'));
            if(input('post.title')){
                $map['title'] = input('post.title');
                $check_user = Pages::get($map);
                if ($check_user) {
                    return $result = ['code'=>0,'msg'=>'标题已存在，请重新输入标题!'];
                }
            }
            //2.验证字段
            $msg = $this->validate($data,'Pages');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }

            Pages::update($data);
            return $result = ['code'=>1,'msg'=>'单页修改成功!','url'=>U('singleList')];
        }else{
            $info = Pages::get(['id'=>input('id')]);
            $this->assign('row', $info);
            return view('singleForm');
        }
    }

    //添加單頁
    public function singleAdd(){
        if(request()->isPost()){
            $data = input('post.');

            //1.验证(标题)是否唯一
            $check_user = Pages::get(['title'=>$data['title']]);
            if ($check_user) {
                return $result = ['code'=>0,'msg'=>'标题已存在，请重新输入标题!'];
            }
            $data['add_time'] = time();

            //2.验证
            $msg = $this->validate($data,'app\Backend\validate\Pages');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }

            //3.添加成功
            if (Pages::create($data)) {
                return ['code'=>1,'msg'=>'單頁添加成功!','url'=>'/reload'];
            } else {
                return ['code'=>0,'msg'=>'單頁添加失败!'];
            }
        }else{
            //展示添加页面
            $this->assign('title',lang('add').lang('admin'));
            $this->assign('info','null');
            $this->assign('selected', 'null');
            return view('singleForm');
        }
    }

    //=========================================== 文章管理 ======================================================

    //公告列表
    public function noticeList(){

        $list = db('user')->order('id desc')->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //=========================================== 广告管理 ======================================================


    //邮箱配置列表
    public function emailList(){

        $list = db('user')->order('id desc')->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //修改订单
    public function orderEdit(){
        $id             = input('id');
        $map['id']      = $id;
        $map['is_sys']  = 0;
        return $result = ['code'=>0,'msg'=>'订单修改失败!','url'=>''];
    }

    //取消订单--状态改为取消
    public function orderEditStatus(){
        $id             = input('id');
        $map['id']      = $id;
        $map['is_sys']  = 0;
        return $result = ['code'=>0,'msg'=>'订单取消失败!','url'=>''];
    }

    //删除订单
    public function orderDel(){
        $id             = input('id');
        $map['id']      = $id;
        if (db('order')->where($map)->delete()){
            return $result = ['code'=>1,'msg'=>'删除成功!','url'=>'/reload'];
        }else{
            return $result = ['code'=>0,'msg'=>'用户信息删除失败!','url'=>''];
        }
    }


}